package io.renren.modules.project.service.impl;

import io.renren.common.exception.RRException;
import io.renren.common.utils.R;
import io.renren.common.utils.RandomId;
import io.renren.modules.oss.constant.UploadConstant;
import io.renren.modules.oss.service.FileUpLoadSrv;
import io.renren.modules.project.entity.ConferenceAccessoryEntity;
import io.renren.modules.project.entity.ConferenceStaffEntity;
import io.renren.modules.project.entity.PojectOperationEntity;
import io.renren.modules.project.service.PojectOperationService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.SysUserTokenEntity;
import io.renren.modules.sys.service.SysUserTokenService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import io.renren.modules.project.dao.ConferenceSummaryDao;
import io.renren.modules.project.entity.ConferenceSummaryEntity;
import io.renren.modules.project.service.ConferenceSummaryService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


@Service("conferenceSummaryService")
public class ConferenceSummaryServiceImpl implements ConferenceSummaryService {
    @Autowired
    private ConferenceSummaryDao conferenceSummaryDao;
    @Autowired
    private SysUserTokenService sysUserTokenService;

    @Override
    public ConferenceSummaryEntity queryObject(String id) {
        return conferenceSummaryDao.queryObject(id);
    }

    @Override
    public List<ConferenceSummaryEntity> queryList(Map<String, Object> map) {
        return conferenceSummaryDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return conferenceSummaryDao.queryTotal(map);
    }

    @Override
    public void save(ConferenceSummaryEntity conferenceSummary) {
        conferenceSummaryDao.save(conferenceSummary);
    }

    @Override
    public void update(ConferenceSummaryEntity conferenceSummary) {
        conferenceSummaryDao.update(conferenceSummary);
    }

    @Override
    public void delete(String id) {
        conferenceSummaryDao.delete(id);
    }

    @Override
    public void deleteBatch(String[] ids) {
        conferenceSummaryDao.deleteBatch(ids);
    }

    @Override
    public List<ConferenceSummaryEntity> queryProjectIdList(String projectId) {

        return conferenceSummaryDao.queryProjectIdList(projectId);
    }

    @Override
    public void addConferenceSummary(ConferenceSummaryEntity conferenceSummary) {
        //String conferenceId = RandomId.createId()+"";
        //接收人实例集合
        List<ConferenceStaffEntity> conferenceStaffList = new ArrayList<>();
        if (conferenceSummary.getConferenceStaff() != null && !"".equals(conferenceSummary.getConferenceStaff())) {
            List<String> userId = Arrays.asList(conferenceSummary.getConferenceStaff().split(","));
            if (userId != null && userId.size() > 0) {
                for (int i = 0; i < userId.size(); i++) {
                    ConferenceStaffEntity conferenceStaff = new ConferenceStaffEntity();
                    conferenceStaff.setId(RandomId.createId()+i + "");
                    conferenceStaff.setMeetingStaff(userId.get(i));
                    //会议ID
                    conferenceStaff.setConferenceId(conferenceSummary.getId());
                    conferenceStaff.setTagging(0);
                    conferenceStaffList.add(conferenceStaff);
                }
            }
            conferenceSummaryDao.saveConferenceStaff(conferenceStaffList);
        }
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //conferenceSummary.setId(conferenceId);
        conferenceSummary.setCreationTime(formatter.format(currentTime));
        conferenceSummaryDao.save(conferenceSummary);
    }

    @Override
    public ConferenceAccessoryEntity queryDetailsMeeting(String id ,HttpServletRequest request) {
        ConferenceAccessoryEntity conferenceAccessoryEntity = new ConferenceAccessoryEntity();
        if (id != null && !"".equals(id)) {
            String getContextPath = request.getContextPath();
            String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+getContextPath+"/";
            SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
            SysUserTokenEntity userToken = sysUserTokenService.queryByUserId(user.getUserId());
            String baseUrl = basePath + UploadConstant.IMAGES_ROUTE + "/";

            //查询该会议的时间
            String detailsMeetingTime = conferenceSummaryDao.queryDetailsMeetingTime(id);
            //查询改会议的参会人员
            List<String> meetingStaff = conferenceSummaryDao.queryConferenceSummaryDao(id);
            //查询该会议的通知附件
            List<ConferenceAccessoryEntity> conferenceMesgAccessory = conferenceSummaryDao.queryConferenceMesgAccessory(id);
            //查询该会议的附件
            List<ConferenceAccessoryEntity> conferenceAccessory = conferenceSummaryDao.queryConferenceAccessory(id);
            if (detailsMeetingTime != null && !"".equals(detailsMeetingTime)){
                conferenceAccessoryEntity.setConferenceTime(detailsMeetingTime);
            }
            if (meetingStaff != null && meetingStaff.size()>0){
                conferenceAccessoryEntity.setMeetingStaff(String.join(",",meetingStaff));
            }
            if (conferenceMesgAccessory != null && conferenceMesgAccessory.size()>0){
                for (int i = 0 ; i<conferenceMesgAccessory.size();i++){
                    String tempPath = StringUtils.substringAfterLast(conferenceMesgAccessory.get(i).getConferenceMesgAccessory(), "/");
                    String imageUrl = baseUrl + tempPath + "?token=" + userToken.getToken();
                    conferenceMesgAccessory.get(i).setConferenceMesgAccessory(imageUrl);
                }
                conferenceAccessoryEntity.setConferenceMesgAccessoryList(conferenceMesgAccessory);
            }
            if (conferenceAccessory != null && conferenceAccessory.size()>0){
                for (int i = 0 ; i<conferenceAccessory.size();i++){
                    String tempPath = StringUtils.substringAfterLast(conferenceAccessory.get(i).getConferenceAccessory(), "/");
                    String imageUrl = baseUrl + tempPath + "?token=" + userToken.getToken();
                    conferenceAccessory.get(i).setConferenceAccessory(imageUrl);
                }
                conferenceAccessoryEntity.setConferenceAccessoryList(conferenceAccessory);
            }
        }
        return conferenceAccessoryEntity;
    }

    @Override
    public List<ConferenceSummaryEntity> mainQueryConferenceSummary(String userId) {
        return conferenceSummaryDao.mainQueryConferenceSummary(userId);
    }


}
